<script>
    import Vue from 'vue';
    export default {
        onLaunch() {
            console.log('App Launch')
            //#ifdef APP-PLUS
            this.updateCid();
            uni.$on('login', (e) => {
                this.updateCid();
            });

            plus.push.setAutoNotification(true);
            //监听接收透传消息事件  
            plus.push.addEventListener('receive', (msg) => {
                this.handlePush(msg);
            }, false);
            //监听系统通知栏消息点击事件 
            plus.push.addEventListener('click', (msg) => {
                this.handlePush(msg, true);
            }, false);
            //#endif
        },
        onShow: function() {
            console.log('App Show')
        },
        onHide: function() {
            console.log('App Hide')
        },
        is_ajax: false,
        is_wait: false,
        methods: {
            handlePush(msg, click) {
                console.log('handlePush');
                console.log(msg);
                console.log(JSON.stringify(msg));
                plus.push.clear();
                uni.$emit('new_msg');
                this.updateBadge();
                if(click) {
                    uni.switchTab({
                        url: '/pages/views/tabBar/msg'
                    });
                }
            },
            updateBadge() {
                if(this.is_ajax) {
                    this.is_wait = true;
                    return false;
                }
                this.is_ajax = true;
                this.is_wait = false;
                this._ajax({
                    url: '/api/user/getUnreadNum',
                    success: res => {
                        if (res.code == 1) {
                            let num = parseInt(res.data) || 0;
                            if (num > 0) {
                                uni.setTabBarBadge({
                                    index: 2,
                                    text: '' + num
                                });
                            } else {
                                uni.removeTabBarBadge({
                                    index: 2
                                });
                            }
                            //#ifdef APP-PLUS
                            plus.runtime.setBadgeNumber(num);
                            //#endif
                        }
                        this.is_ajax = false;
                        if(this.is_wait) this.updateBadge();
                    }
                });
            },
            updateCid() {
                plus.push.getClientInfoAsync((info) => {
                    console.log('Success');
                    console.log(JSON.stringify(info));
                    if(info.clientid) {
                        uni.setStorageSync('cid', info.clientid);
                        var user = uni.getStorageSync('user');
                        if(user && user.cid != info.clientid) {
                            this._ajax({
                                url: '/api/user/updateCid',
                                data: {
                                    cid: info.clientid
                                },
                                success: res => {
                                    if(res.code == 1) {
                                        uni.setStorageSync('user', res.data);
                                    }
                                }
                            });
                        }
                    }
                }, function(e) {
                    console.log('Failed');
                    console.log(JSON.stringify(e));
                });
            }
        }
    }
</script>

<style lang="scss">
    @import "@/uni_modules/uview-ui/index.scss";
</style>
<style>
    @import "./app.css";
    @import "./static/css/animate.css";
</style>
